/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package au.com.domlogic.helloPVCWorld;

import au.com.domlogic.pvc.core.element.PVCElement;
import au.com.domlogic.pvc.core.servlet.LocalRedirectionResponseWrapper;
import au.com.domlogic.pvc.xhtml.servlet.XHTMLStrictRequestWrapper;
import au.com.domlogic.pvc.xhtml.servlet.XHTMLStrictServlet;
import au.com.domlogic.pvc.xhtml.document.XHTMLStrictDocument;
import au.com.domlogic.pvc.xhtml.element.H1ElementMold;
import au.com.domlogic.pvc.xhtml.element.LinkElementMold;
import au.com.domlogic.pvc.xhtml.element.builder.CSSLinkMoldBuilder;
import au.com.domlogic.pvc.xhtml.serialization.XHTMLStrictSerializer;
import au.com.domlogic.helloPVCWorld.builder.ExampleHeadingMoldBuilder;
import au.com.domlogic.helloPVCWorld.builder.ExampleTreeViewBuilder;
import au.com.domlogic.pvc.core.component.ComponentMold;
import au.com.domlogic.pvc.xhtml.component.builder.ComponentTreeMoldBuilder;
import au.com.domlogic.pvc.xhtml.element.DivElementMold;
import au.com.domlogic.pvc.xhtml.element.PElementMold;
import au.com.domlogic.pvc.xhtml.element.UlElementMold;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.xml.transform.TransformerException;

/**
 * In this example servlet, I create a {@link ComponentMold} and present its
 * structure using an extension of {@link ComponentTreeMoldBuilder} which I
 * have created called {@link ExampleTreeViewBuilder}.
 * 
 * @author Domenic Figliomeni
 */
public class ExampleServlet extends XHTMLStrictServlet {

    @Override
    protected void doGet(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {

        XHTMLStrictDocument document = request.getDocument();
        PVCElement body = document.getBody();
        PVCElement head = document.getHead();
        PVCElement title = document.getTitle();
        //Set the document title
        title.setTextContent("An Example");

        //Decorate the body with our heading
        ExampleHeadingMoldBuilder builder = new ExampleHeadingMoldBuilder();
        builder.setLocale(request.getLocale());
        H1ElementMold exampleHeadingElementMold = builder.buildElementMold();
        body = exampleHeadingElementMold.new ElementDecorator(body);

        //Decorate the head with a link to the stylesheet
        CSSLinkMoldBuilder cssLinkBuilder = new CSSLinkMoldBuilder("css/example.css");
        LinkElementMold cssLink = cssLinkBuilder.buildElementMold();
        head = cssLink.new ElementDecorator(head);

        //Show the tree structure of a component
        ComponentMold subject = new ComponentMold();
        subject.addComponentMold(exampleHeadingElementMold);
        subject.addComponentMold(new DivElementMold().addComponentMold(
                new PElementMold().setContent("A paragraph nested inside a div")));
        //Create a branch with another component
        ComponentMold branch = new ComponentMold();
        subject.addComponentMold(branch);
        branch.addComponentMold(new PElementMold().setContent("A paragraph"));
        branch.addComponentMold(new DivElementMold().setContent("Some content"));
        ExampleTreeViewBuilder treeBuilder = new ExampleTreeViewBuilder();
        treeBuilder.setComponentMold(subject);
        treeBuilder.setShowTextContent(true);
        UlElementMold treeMold = treeBuilder.buildComponentMold();
        body = treeMold.new ElementDecorator(body);

        //Call the decorator method gluePVC() on the PVCElements to execute the decorator functionality
        head.gluePVC();
        body.gluePVC();

        //Serialize
        try {
            XHTMLStrictSerializer.serialize(document, response.getOutputStream());
        }
        catch (TransformerException e) {
            throw new ServletException(e);
        }
    }
}
